<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/zTree/css/zTreeStyle/zTreeStyle.css" media="all">
    <script type="text/javascript" src="/zTree/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="/zTree/js/jquery.ztree.core.js"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" >
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required" >角色名</label>
        <div class="layui-input-block">
            <input type="hidden" name="id"  id="id"  th:value="${param.id}" class="layui-input">
            <input type="text" name="name"  id="name" th:value="${param.name}" class="layui-input">
            <tip>修改角色名</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="status" th:if="${status} eq 0"   class="city-selector">
                <option  th:if="${status} eq 0" th:text="启用" value="1"></option>
                <option  value="1">禁用</option>
            </select>
            <select name="status" th:if="${status} eq 1"   class="city-selector">
                <option  th:if="${status} eq 1" th:text="禁用" value="1"></option>
                <option  value="0">启用</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label ">修改权限</label>
        <div class="layui-input-inline" style="width: 200px;">
            <div id="tree" class="demo-tree"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
    </div>
</form>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','tree', 'util'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            util = layui.util,
            $ = layui.$;

        //初始化树形结构
        $.ajax({
            url:'/menuList',
            success:function(data){
                tree.render({
                    elem: '#tree'
                    ,data: data
                    ,showCheckbox: true
                    ,id:'id'
                });
            }
        })

        //获取选中节点的ID
        function getChecked_list(data) {
            var id = '';
            $.each(data, function (index, item) {
                if (id != '') {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != '') {
                    id = id + "," + i;
                }
            });
            return id;
        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var checkData = tree.getChecked('id');
            var list = new Array();
            list = getChecked_list(checkData);
            $.ajax({
                url:"/updateRole?id="+data.field.id+"&list="+list,
                type:"post",
                dataType:"json",
                contentType:"application/json",
                data:JSON.stringify(data.field),
                success:function (data) {
                    if(data==1){
                        layer.msg("修改角色成功")
                    }else{
                        layer.msg("修改角色失败")
                    }
                }
            })
        });

            return false;

    });



</script>
</body>
</html>